const { queryOrderBPage, queryAftersalePage } = require('../../services/order')
const validate = require('../../utils/validate')
const common = require('../../utils/common')

Page({
  data: {
    value: '',
    type: 1, // 1全部订单 2售后订单
  },
  onLoad: function ({ type }) {
    this.setData({ type })
  },
  inputChange: function (e) {
    this.setData({
      value: e.detail.value
    })
  },
  //下一步
  onBack: function () {
    //返回
    if (this.data.type == 1) {
      wx.navigateTo({ url: `/pages/all-order-list/index?searchNo=${this.data.value}` })
    } else {
      wx.navigateTo({ url: `/pages/sell-order-list/index?searchNo=${this.data.value}` })
    }
  },
  search: function (e) {
    const searchNo = this.data.value
    const requestFn = this.data.type != 1 ? queryAftersalePage : queryOrderBPage
    const errorMsg = validate.isPhone(searchNo) ? '手机号不存在' : '订单号不存在'
    requestFn({ searchNo, curPage: 1, pageSize: 2 }).then(res => {
      if (!res.data || res.data.length <= 0) {
        common.showToast(errorMsg)
      } else {
        this.onBack()
      }
    })
  }
})
